package com.culture.bootdemo.utils;

import com.culture.bootdemo.po.Answer;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author: wen
 * @date 2020/10/19
 */
public class DateUtil {

    public static final String YMDHMS_STR = "yyyyMMddHHmmss";
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    //eg: 2024-6-1 9:12:3
    public static final String YYYY_M_D_H_M_S = "yyyy-M-d H:m:s";

    public static String formatNow(String format) {
        LocalDateTime now = LocalDateTime.now();
        return now.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 将java.util.Date 按指定格式转化为String
     *
     * @param date
     * @param format
     * @return
     */
    public static String date2str(Date date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
        return sdf.format(date);
    }

    /**
     * 将string 按指定格式转化为java.util.Date
     *
     * @param str
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date str2Date(String str, String format) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 获取当前时间
     * code 1年，2月，3星期，5日，11小时，12分钟，13秒，14毫秒
     * time 操作数值
     */
    public static Date getTimeDateAdd(int code, Integer time) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(code, time);
        return calendar.getTime();
    }

    /**
     * 指定时间往前或往后推n天
     *
     * @param specifiedDay 指定时间
     * @param x            指定天数
     * @return
     */
    public static String getDayBefore(String specifiedDay, int x) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - x);    //往前推几天
        //c.set(Calendar.DATE, day + x);  往后推几天

        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayBefore;
    }

    /**
     * 获取当天起始时间戳
     *
     * @return Date
     */
    public static Date getCurrentStartDateTime(Date date) {

        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 0);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        dateStart.set(Calendar.MILLISECOND, 0);
        return dateStart.getTime();
    }

    /**
     * 获取当天结束时间戳
     *
     * @return Date
     */
    public static Date getCurrentEndDateTime(Date date) {

        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date);
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        dateEnd.set(Calendar.MILLISECOND, 0);
        return dateEnd.getTime();
    }




    public static void main(String[] args) {
        System.out.println(getDayBefore("2022-12-20", 365));
    }
}
